package com.bmob.lover.ui.fragment.user

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.qmuiteam.qmui.widget.QMUIRadiusImageView2
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView
import com.theone.common.constant.BundleConstant
import com.theone.common.ext.formatString
import com.theone.common.ext.getValueNonNull
import com.theone.common.ext.hidePhoneNumber
import com.theone.common.ext.visible
import com.bmob.lover.R
import com.bmob.lover.data.model.bean.User
import com.bmob.lover.databinding.FragmentUserInfoBinding
import com.bmob.lover.ui.fragment.base.BaseFragment
import com.bmob.lover.viewmodel.state.UserInfoViewModel
import com.theone.mvvm.core.app.util.glide.GlideUtil
import com.theone.mvvm.ext.qmui.addToGroup
import com.theone.mvvm.ext.qmui.createCustomViewItem
import com.theone.mvvm.ext.qmui.createItem
import com.theone.mvvm.ext.qmui.setTitleWitchBackBtn
import com.zhpan.bannerview.transform.toPx

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 * @author The one
 * @date 2021-06-08 09:02
 * @describe 用户信息
 * @email 625805189@qq.com
 * @remark
 */
class UserInfoFragment private constructor() :
    BaseFragment<UserInfoViewModel, FragmentUserInfoBinding>(), View.OnClickListener {

    companion object {
        fun newInstance(bean: User): UserInfoFragment {
            return UserInfoFragment().apply {
                arguments = Bundle().apply {
                    putParcelable(BundleConstant.DATA, bean)
                }
            }
        }
    }

    private lateinit var mID: QMUICommonListItemView
    private lateinit var mIcon: QMUICommonListItemView
    private lateinit var mNickName: QMUICommonListItemView
    private lateinit var mSex: QMUICommonListItemView
    private lateinit var mPhoneNumber: QMUICommonListItemView
    private lateinit var mBirthday: QMUICommonListItemView
    private lateinit var mBind: QMUICommonListItemView

    private val mUser: User by getValueNonNull(BundleConstant.DATA)

    private val ivIcon: QMUIRadiusImageView2 by lazy {
        QMUIRadiusImageView2(mActivity).apply {
            val width = 45.toPx()
            layoutParams = ViewGroup.LayoutParams(width, width)
            scaleType = ImageView.ScaleType.CENTER_CROP
            isCircle = true
            borderWidth = 0
        }
    }

    override fun initView(root: View) {

        with(getDataBinding().mGroupListView) {
            mID = createItem("ID", detail = mUser.objectId)
            mIcon = createCustomViewItem("头像", view = ivIcon)
            mNickName = createItem("昵称")
            mSex = createItem("性别")
            mPhoneNumber = createItem("手机号码", mUser.mobilePhoneNumber?.run {
                if (mUser.mobilePhoneNumber.isNullOrEmpty()) "未绑定" else hidePhoneNumber()
            })
            mBirthday = createItem("生日")
            mBind = createItem("绑定另一半")

            addToGroup(
                mID,
                mIcon,
                mNickName,
                mSex,
                mPhoneNumber,
                mBirthday,
                title = "",
                listener = this@UserInfoFragment
            )
            addToGroup(mBind, title = "", listener = this@UserInfoFragment)
        }
        mAppVm.getUserInfoLiveData().value?.run {
            val isMine = mUser.objectId == objectId
            val isBindLover = null != mAppVm.getLoverBindLiveData().value
            val title = when {
                !isBindLover -> "个人信息"
                isMine -> "执子之手"
                else -> "与子偕老"
            }
            if (isMine) {
                getTopBar()?.addRightTextButton("修改", R.id.topbar_right_text)?.setOnClickListener {
                    startFragment(UserInfoModifyFragment.newInstance())
                }
            }
            mBind.visible(isMine && !isBindLover)
            setTitleWitchBackBtn(title)
        }
        mUser.setUserInfo()
    }

    override fun createObserver() {
        mAppVm.getUserInfoLiveData().observe(this) {
            it.setUserInfo()
        }
    }

    private fun User.setUserInfo() {
        GlideUtil.load(mActivity, avatar, ivIcon)
        mNickName.detailText = nickname
        mSex.detailText = sex()
        mBirthday.detailText = birthday?.getDate()?.formatString()
    }

    override fun onClick(v: View?) {
        when (v) {
            mBind -> {
                startFragment(BindLoverFragment())
            }
        }
    }

}